package com.yu.rpc.core.protocol;

import com.yu.rpc.core.RpcApplication;
import com.yu.rpc.core.config.RpcConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义网络协议
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProtocolMessage<T> {
    private Header header;
    private T body;

    @Data
    public static class Header {
        private byte magic;
        private byte version;
        private byte serializer;
        private byte type;
        private byte status;
        private long requestId;
        private int bodyLength;

        public static Header defaultHeader() {
            Header header = new Header();
            header.setMagic(ProtocolConstant.PROTOCOL_MAGIC);
            header.setVersion(ProtocolConstant.PROTOCOL_VERSION);
            RpcConfig config = RpcApplication.getConfig();
            ProtocolMessageSerializerEnum serializerEnum = ProtocolMessageSerializerEnum.getEnumByValue(config.getSerializer());
            if (serializerEnum != null) {
                int serializerKey = serializerEnum.getKey();
                header.setSerializer((byte) serializerKey);
            }
            return header;
        }
    }
}
